<!--
  用户管理-企业用户-身份认证页面
 -->
<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="待审核" name="1">
        <!-- 搜索工作栏 -->
        <el-form
          ref="leftQueryForm"
          :model="leftQueryParams"
          size="small"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="手机号码" prop="mobile">
            <el-input
              v-model="leftQueryParams.mobile"
              placeholder="请输入手机号码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="学生姓名" prop="studentName">
            <el-input
              v-model="leftQueryParams.studentName"
              placeholder="请输入学生姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-refresh"
              @click="resetQuery"
            >重置</el-button>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
            >搜索</el-button>
          </el-form-item>
        </el-form>
        <!-- 列表 -->
        <el-table v-loading="loading" :data="leftList">
          <el-table-column label="序号" type="index" />
          <el-table-column
            label="认证材料"
            align="center"
            prop="createTime"
            width="250"
          >
            <template slot-scope="scope">
              <Thumbnail
                :url="scope.row.authImages[0]"
                :list="scope.row.authImages"
                :size="124"
              />
            </template>
          </el-table-column>
          <el-table-column label="学生姓名" align="center" prop="studentName" />
          <el-table-column label="手机号" align="center" prop="mobile" />
          <el-table-column label="国籍" align="center" prop="studentAbroad">
            <template slot-scope="scope">
              <span>{{ scope.row.studentAbroad === 1 ? '中国内地学生' : '非中国内地学生' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="提交时间"
            align="center"
            prop="postDate"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.postDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <!-- 学生认证状态 studentAuthStatus 0 默认状态， 1 认证通过， 2 认证驳回 3 认证中 -->
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleCheck(scope.row)"
              >查看审核资料</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row, '1')"
              >审核通过</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row, '2')"
              >审核驳回</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination
          v-show="leftTotal > 0"
          :total="leftTotal"
          :page.sync="leftQueryParams.pageNo"
          :limit.sync="leftQueryParams.pageSize"
          @pagination="leftGetList"
        />
      </el-tab-pane>
      <el-tab-pane label="已审核" name="2">
        <!-- 搜索工作栏 -->
        <el-form
          ref="rightQueryForm"
          :model="rightQueryParams"
          size="small"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="学生姓名" prop="studentName">
            <el-input
              v-model="rightQueryParams.studentName"
              placeholder="请输入学生姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile">
            <el-input
              v-model="rightQueryParams.mobile"
              placeholder="请输入手机号码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="审核结果" prop="checkStatus">
            <el-select
              v-model="rightQueryParams.checkStatus"
              placeholder="请选择审核状态"
              clearable
              size="small"
            >
              <!-- 1 认证通过， 2 认证驳回 -->
              <el-option
                v-for="dict in studentAuthStatus"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="审核人" prop="checker">
            <el-input
              v-model="rightQueryParams.checker"
              placeholder="请输入审核人"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-refresh"
              @click="resetQuery"
            >重置</el-button>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
            >搜索</el-button>
          </el-form-item>
        </el-form>
        <!-- 列表 -->
        <el-table v-loading="loading" :data="rightList">
          <el-table-column label="序号" type="index" />
          <el-table-column
            label="认证材料"
            align="center"
            prop="createTime"
            width="250"
          >
            <template slot-scope="scope">
              <Thumbnail
                :url="scope.row.authImages[0]"
                :list="scope.row.authImages"
                :size="124"
              />
            </template>
          </el-table-column>
          <el-table-column label="学生姓名" align="center" prop="nickname" />
          <el-table-column label="手机号" align="center" prop="mobile" />
          <el-table-column label="国籍" align="center" prop="studentAbroad">
            <template slot-scope="scope">
              <span>{{ scope.row.studentAbroad === 1 ? '中国内地学生' : '非中国内地学生' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="提交时间"
            align="center"
            prop="postDate"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.postDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核时间"
            align="center"
            prop="checkDate"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.checkDate) }}</span>
            </template>
          </el-table-column>
          <!-- 学生认证状态 studentAuthStatus 0 默认状态， 1 认证通过， 2 认证驳回 3 认证中 -->
          <el-table-column label="审核结果" align="center" prop="status">
            <template slot-scope="scope">
              {{ Number(scope.row.status) === 1 ? "通过" : "驳回" }}
            </template>
          </el-table-column>
          <el-table-column label="审核人" align="center" prop="checker" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleCheck(scope.row)"
              >查看审核资料</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination
          v-show="rightTotal > 0"
          :total="rightTotal"
          :page.sync="rightQueryParams.pageNo"
          :limit.sync="rightQueryParams.pageSize"
          @pagination="rightGetList"
        />
        <!-- 查看学生审核信息 -->
        <el-dialog
          title="身份认证"
          :visible.sync="openuserInfo"
          width="50%"
          append-to-body
        >
          <el-descriptions title="" :column="1">
            <el-descriptions-item label="姓名">{{ userInfo.nickname || '--' }}</el-descriptions-item>
            <el-descriptions-item label="手机号码">
              {{ userInfo.mobile || '--' }}
              <el-tag v-if="!userInfo.authenticationCardFrontPictureUrl" style="margin-left: 12px;">非大陆手机</el-tag>
            </el-descriptions-item>
            <el-descriptions-item v-if="!userInfo.authenticationCardFrontPictureUrl" label="认证邮箱">
              {{ userInfo.email || '--' }}
            </el-descriptions-item>
          </el-descriptions>
          <!-- studentAbroad  学生类型;1:国内,2:国外 -->
          <el-descriptions v-if="userInfo.studentAbroad === 1 && userInfo.authenticationCardFrontPictureUrl" title="身份证信息" :column="1">
            <el-descriptions-item label="身份证号码">{{ userInfo.cardNo || '--' }}</el-descriptions-item>
            <el-descriptions-item label="" label-class-name="labels" content-class-name="contents">
              <div class="imgBox">
                <el-image
                  style="height: 120px"
                  :src="userInfo.authenticationCardFrontPictureUrl"
                  :preview-src-list="[userInfo.authenticationCardFrontPictureUrl]"
                />
                <div style="margin-top: 12px;text-align: center">身份证正面照片</div>
              </div>
              <div class="imgBox">
                <el-image
                  style="height: 120px"
                  :src="userInfo.authenticationCardBackPictureUrl"
                  :preview-src-list="[ userInfo.authenticationCardBackPictureUrl]"
                />
                <div style="margin-top: 12px;text-align: center">身份证反面照片</div>
              </div>
            </el-descriptions-item>
          </el-descriptions>
          <el-descriptions v-if="userInfo.studentAbroad === 2" title="护照信息" :column="1">
            <el-descriptions-item label="护照号码">{{ userInfo.passport || '--' }}</el-descriptions-item>
            <el-descriptions-item label="" label-class-name="labels" content-class-name="contents">
              <div class="imgBox">
                <el-image
                  style="height: 120px"
                  :src="userInfo.authenticationCardFrontPictureUrl"
                  :preview-src-list="[userInfo.authenticationCardFrontPictureUrl]"
                />
                <div style="margin-top: 12px;text-align: center">护照正面照片</div>
              </div>
              <div class="imgBox">
                <el-image
                  style="height: 120px"
                  :src="userInfo.authenticationCardBackPictureUrl"
                  :preview-src-list="[ userInfo.authenticationCardBackPictureUrl]"
                />
                <div style="margin-top: 12px;text-align: center">护照反面照片</div>
              </div>
            </el-descriptions-item>
          </el-descriptions>
          <el-descriptions title="学生证信息" :column="1">
            <el-descriptions-item label="" label-class-name="labels" content-class-name="contents">
              <div class="imgBox">
                <el-image
                  style="height: 120px"
                  :src="userInfo.authenticationFrontPictureUrl"
                  :preview-src-list="[userInfo.authenticationFrontPictureUrl]"
                />
                <div style="margin-top: 12px;text-align: center">证件正面照片</div>
              </div>
              <div class="imgBox">
                <el-image
                  style="height: 120px"
                  :src="userInfo.authenticationBackPictureUrl"
                  :preview-src-list="[ userInfo.authenticationBackPictureUrl]"
                />
                <div style="margin-top: 12px;text-align: center">证件反面照片</div>
              </div>
            </el-descriptions-item>
          </el-descriptions>
          <el-descriptions v-if="userInfo.studentAbroad === 1 && userInfo.authenticationCardFrontPictureUrl" title="阿里认证信息" :column="1">
            <el-descriptions-item label="" label-class-name="labels">
              <el-tag v-if="isAuth" type="success">实名认证成功</el-tag>
              <el-tag v-if="!isAuth" type="danger">实名认证失败:{{ authText }}</el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </el-dialog>
        <!-- 通过\驳回审核 -->
        <el-dialog
          :title="checkStatus=='1'?'确定通过吗':'确定驳回吗'"
          :visible.sync="showCheck"
          width="50%"
          append-to-body
        >
          <el-descriptions v-if="rowObj" title="" :column="1">
            <el-descriptions-item label="姓名">{{ rowObj.nickname || '--' }}</el-descriptions-item>
            <el-descriptions-item label="手机号码">{{ rowObj.mobile || '--' }}</el-descriptions-item>
            <el-descriptions-item v-if="(!rowObj.authenticationCardFrontPictureUrl&&checkStatus=='1')" label="认证邮箱">
              {{ rowObj.email || '--' }}
            </el-descriptions-item>
            <span v-if="checkStatus=='1'" style="color: #909399">确定后，将发送认证链接至该邮箱</span>
          </el-descriptions>
          <div class="formBox" :hidden="checkStatus=='1'">
            <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
              <el-form-item label="驳回理由" prop="remark" required>
                <el-input v-model="ruleForm.remark" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" placeholder="请填写该学生驳回理由" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="checkStudentFun">确定</el-button>
                <el-button @click="showCheck=!showCheck">取消</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="formBox" :hidden="checkStatus=='2'">
            <el-button type="primary" @click="checkStudentFun">确定</el-button>
            <el-button @click="showCheck=!showCheck">取消</el-button>
          </div>
        </el-dialog>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  getStudentPage,
  checkStudent,
  getStudentInfo
} from '@/api/school-admin-api/userManage/student/index.js'
import { businessAreaData } from '@/utils/frontDataDict.js'
import { deepClone } from '@/utils/index'
import { isRealName } from '@/api/login.js'
export default {
  name: 'Company',
  components: {
  },
  data() {
    return {
      // 学生认证状态 studentAuthStatus 0 默认状态， 1 认证通过， 2 认证驳回 3 认证中
      studentAuthStatus: [
        {
          label: '通过',
          value: '1'
        },
        {
          label: '驳回',
          value: '2'
        }
      ],
      srcList: [
        'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
        'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
        'https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg'
      ],
      activeCheck: 'userInfo',
      checkResult: '1',
      activeName: '1',
      openuserInfo: false,
      rowObj: null,
      checkStatus: '1',
      showCheck: false,
      rules: {
        remark: [
          { required: true, message: '请填写该学生驳回理由', trigger: 'blur' }
        ]
      },
      ruleForm: {
        remark: ''
      },
      // 企业行业类型数据，二级联动
      businessAreaData: businessAreaData,
      // 遮罩层
      loading: true,
      // 待审核-总条数
      leftTotal: 0,
      // 已审核-总条数
      rightTotal: 0,
      // 企业管理列表
      leftList: [],
      // 已审核企业列表
      rightList: [],
      // 弹出层标题
      title: '',
      // 待审核查询参数
      leftQueryParams: {
        pageNo: 1,
        pageSize: 10,
        studentName: '',
        mobile: '',
        status: [3] // 学生认证状态 studentAuthStatus 0 默认状态， 1 认证通过， 2 认证驳回 3 认证中
      },
      rightQueryParams: {
        pageNo: 1,
        pageSize: 10,
        studentName: '',
        mobile: '',
        checker: '',
        checkStatus: '',
        status: [1, 2] // 学生认证状态 studentAuthStatus 0 默认状态， 1 认证通过， 2 认证驳回 3 认证中
      },
      // 当前审核数据
      currentCheckRow: null,
      // 用户信息表
      userInfo: {
        applyUserName: '',
        dept: '',
        job: '',
        tel: '',
        email: '',
        companyName: '',
        phoneNumber: ''
      },
      // 企业信息表
      companyForm: {
        creditCode: '', // 统一信用代码
        businessLicense: '', // 营业执照
        industryType: [], // 行业类型
        postCode: '', // 邮政编码
        businessStatus: '', // 经营状态
        registeredCapital: '', // 注册资本
        establishDate: '', // 成立日期
        legalPerson: '', // 法人
        subCompanyRelationshipCertificatePicture: '', // 子母企业关系证明
        scale: '', // 企业规模
        companyType: '', // 企业类型
        registeredAddress: '', // 注册地址
        businessScope: '' // 经营范围
      },
      canClick: true,
      isAuth: false,
      authText: ''

    }
  },
  created() {
    this.leftGetList()
    this.rightGetList()
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    handleClick(data) {
      this.handleQuery()
    },
    // 请求待审核列表数据
    leftGetList() {
      this.loading = true
      // 处理查询参数
      const params = { ...this.leftQueryParams }
      for (const key in params) {
        if (params[key] === null || params[key] === '') {
          delete params[key]
        }
      }
      // 执行查询
      getStudentPage(params).then((res) => {
        if (res.code === 0 && res.data) {
          res.data.list = res.data.list.map((item) => {
            item.authImages = []
            if (item.authenticationFrontPictureUrl) {
              item.authImages.push(item.authenticationFrontPictureUrl)
            }
            if (item.authenticationBackPictureUrl) {
              item.authImages.push(item.authenticationBackPictureUrl)
            }
            return item
          })
          this.leftList = res.data.list
          this.leftTotal = res.data.total
          this.loading = false
        }
      })
    },
    /** 请求已审核列表数据 */
    rightGetList() {
      this.loading = true
      // 处理查询参数
      const params = { ...this.rightQueryParams }
      console.log('rightQueryParams>>>', this.rightQueryParams)
      if (params.checkStatus !== '') {
        params.status = [Number(params.checkStatus)]
      }
      for (const key in params) {
        if (params[key] === null || params[key] === '') {
          delete params[key]
        }
      }
      // 执行查询
      getStudentPage(params).then((res) => {
        if (res.code === 0 && res.data) {
          res.data.list = res.data.list.map((item) => {
            item.authImages = []
            if (item.authenticationFrontPictureUrl) {
              item.authImages.push(item.authenticationFrontPictureUrl)
            }
            if (item.authenticationBackPictureUrl) {
              item.authImages.push(item.authenticationBackPictureUrl)
            }
            return item
          })
          this.rightList = res.data.list
          this.rightTotal = res.data.total
        }
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.activeName === '1') {
        this.leftQueryParams.pageNo = 1
        this.leftGetList()
      } else {
        this.rightQueryParams.pageNo = 1
        this.rightGetList()
      }
    },
    // 重制搜索条件
    resetQuery() {
      if (this.activeName === '1') {
        this.resetForm('leftQueryForm')
        this.handleQuery()
      } else {
        this.resetForm('rightQueryForm')
        this.handleQuery()
      }
    },

    // 查看学生个人信息
    handleCheck(row) {
      this.isCheck = true
      this.currentRow = JSON.parse(JSON.stringify(row))
      const { id } = row
      getStudentInfo({ id }).then((res) => {
        console.log('res.data>>', res.data)
        if (res.code === 0 && res.data) {
          this.userInfo = deepClone(res.data)
          this.isAuth = false
          this.authText = ''
          if (this.userInfo.studentAbroad === 1) {
            isRealName({
              cardNo: this.userInfo.cardNo,
              phone: this.userInfo.mobile,
              userName: this.userInfo.nickname
            }).then(res => {
              const { code, message } = res.data
              if (Number(code) === 1) {
                this.isAuth = true
              } else {
                this.isAuth = false
              }
              this.authText = message
              this.openuserInfo = true
            })
          } else {
            this.openuserInfo = true
          }
        }
      })
    },
    /** 通过/驳回 */
    checkStudentFun() {
      const params = this.rowObj
      params.status = this.checkStatus
      params.ismail = 0
      if (!this.canClick) return
      this.canClick = false
      if (Number(params.status) === 1) {
        // 同意
        if (!params.authenticationCardFrontPictureUrl) {
          params.ismail = 1
        }
        checkStudent(params).then((res) => {
          if (res.code === 0 && res.data) {
            this.$message.success('操作成功')
            this.leftGetList()
            this.showCheck = false
          }
          this.canClick = true
        }).catch(() => {
          this.canClick = true
        })
        return
      }
      // 驳回，必填原因
      params.remark = this.ruleForm.remark
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          checkStudent(params).then((res) => {
            if (res.code === 0 && res.data) {
              this.$message.success('操作成功')
              this.leftGetList()
              this.showCheck = false
            }
            this.canClick = true
          }).catch(() => {
            this.canClick = true
          })
        } else {
          this.canClick = true
          console.log('error submit!!')
          return false
        }
      })
    },
    /** 审核 */
    handleUpdate(row, status) {
      const { id } = row
      getStudentInfo({ id }).then((res) => {
        if (res.code === 0 && res.data) {
          this.rowObj = deepClone(res.data)
          this.checkStatus = status
          this.showCheck = true
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  ::v-deep .p--t-8>div{
    flex-wrap: nowrap !important;
  }
  .formBox{
    ::v-deep .el-form-item__label{
      text-align: left;
    }
  }
  ::v-deep .el-descriptions-item__container{
    line-height: 36px;
    vertical-align: middle;
    float: left;
    font-size: 14px;
    color: #606266;
    line-height: 40px;
    padding: 0 12px 0 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  ::v-deep span.labels{
    display: none;
  }
  ::v-deep .el-descriptions-item__container{
    padding: 0;
    width: 100%;
  }
  ::v-deep span.contents{
    display: flex;
    justify-content: space-around;
    padding: 0;
    width: 100%;
  }
</style>
